using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint;
using System.IO;
using Microsoft.SharePoint.Publishing;

namespace LanguageConfigurationSolution
{
    class LanguageConfigurationRootLandingPageFeatureReceiver : SPFeatureReceiver
    {
        private const string ROOTLANDINGPAGEFILE = "RootLandingPage.aspx";

        public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {
            SPSite site = properties.Feature.Parent as SPSite;
            if (site == null) return;

            string path = Path.Combine(properties.Definition.RootDirectory, ROOTLANDINGPAGEFILE);
            FileStream filestream = new FileStream(path, FileMode.Open, FileAccess.Read);
            try
            {
                SPFile file = site.RootWeb.RootFolder.Files.Add(ROOTLANDINGPAGEFILE, filestream, true);

                if (PublishingWeb.IsPublishingWeb(site.RootWeb))
                {
                    PublishingWeb pweb = PublishingWeb.GetPublishingWeb(site.RootWeb);
                    pweb.DefaultPage = file;
                    pweb.Update();
                } else
                {
                    SPFolder folder = site.RootWeb.RootFolder;
                    folder.WelcomePage = file.Url;
                    folder.Update();
                }
            }
            finally
            {
                filestream.Close();
            }

        }

        public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
        {
            SPSite site = properties.Feature.Parent as SPSite;
            if (site == null) return;


            SPFile file = null;
            try
            {
                file = site.RootWeb.RootFolder.Files[ROOTLANDINGPAGEFILE];
                if (file != null)
                {
                    if (PublishingWeb.IsPublishingWeb(site.RootWeb))
                    {
                        PublishingWeb pweb = PublishingWeb.GetPublishingWeb(site.RootWeb);
                        if (file != null && pweb != null && pweb.DefaultPage != null && pweb.DefaultPage.UniqueId == file.UniqueId)
                        {
                            SPFile otherfile = null;
                            foreach (PublishingPage page in pweb.GetPublishingPages())
                            {
                                if (page.ListItem.File != null && page.ListItem.File.UniqueId != file.UniqueId)
                                {
                                    otherfile = page.ListItem.File;
                                    break;
                                }
                            }

                            pweb.DefaultPage = otherfile;
                            pweb.Update();
                        }
                    }
                    else
                    {
                        SPFolder folder = site.RootWeb.RootFolder;
                        folder.WelcomePage = null;
                        folder.Update();
                    }

                    file.Delete();
                }
            }
            catch
            {
                
            }

        }

        public override void FeatureInstalled(SPFeatureReceiverProperties properties)
        {

        }

        public override void FeatureUninstalling(SPFeatureReceiverProperties properties)
        {

        }
    }
}
